// templates/deliver/pages/shipped/shipped.js
import Notify from '@vant/weapp/notify/notify';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        city_name: '',
        orderLocation: [], //后端定位数据
        longitude: 0,
        latitude: 0,
        image: '/templates/deliver/img/business.png',
        activeNames: ['1'],// 打开信息 按钮
        orderAll: [] , //后端订单数据
        view_delivery_man_id: 0,
        view_driver_name: '',
        view_delivery_district_id: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        let view_delivery_man_id = wx.getStorageSync('view_delivery_man_id')
        let view_driver_name = ''
        let view_delivery_district_id = 0
        if (view_delivery_man_id > 0) {
            view_driver_name = wx.getStorageSync('view_driver_name')
            view_delivery_district_id = wx.getStorageSync('view_delivery_district_id')
        }

        var _this = this;
        wx.getLocation({
            type: "gcj02",
            success(res) {
                _this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                 })
            }
        }),
        this.setData({

            city_name: options.city_name,
            view_delivery_man_id,
            view_driver_name,
            view_delivery_district_id
        })
      
        this.selectShipped();
    },
    /**
     * 页面加载查询订单信息
     */
    async selectShipped() {
        var city_name = this.data.city_name; //获取司机名称
        var mall_id = app.globalData.mall_id; //获取主商城id
        var _this = this;

        let view_delivery_man_id = wx.getStorageSync('view_delivery_man_id') || 0
        let view_driver_name = ''
        let view_delivery_district_id = 0
        if (view_delivery_man_id > 0) {
            view_driver_name = wx.getStorageSync('view_driver_name')
            if (view_driver_name.length > 0) {
                city_name = view_driver_name
            }
        }

        const {
            data: res
        } = await wx.p.request({
            url: app.globalData.URL + '/deliver.Order/getBynameShippedOrder',
            method: "GET",
            data: {
                city_name: city_name,
                mall_id: mall_id
            },
        })

        var listData = res.data;

        var orderAll = []; //后端订单数据
        var markers = []; //订单地图标注
       
        if(!listData){
            Notify('没有数据！！');
            return false
        }
        for (var i = 0; i < listData.length; i++) {
            markers[i] = ({
                iconPath: "../../img/icon-hc.png",
                id: listData[i].id,
                latitude: Number(listData[i].latitude),
                longitude: Number(listData[i].longitude),
                width: 23,
                height: 23,
                callout: {
                    content: listData[i].name,
                    fontSize: 14,
                    padding: true,
                    color: '#ffffff',
                    display: 'ALWAYS',
                    textAlign: 'center',
                    borderRadius: 10,
                    bgColor: '#0077ffbe'
                },
            })
            //重组后端数据
            orderAll[i] = {
                address: listData[i].address,
                created_at: listData[i].created_at,
                id: listData[i].id,
                latitude: listData[i].longitude,
                longitude: listData[i].latitude,
                mobile: listData[i].mobile,
                name: listData[i].name,
                order_no: listData[i].order_no,
                remark: listData[i].remark,
                sale_status: listData[i].sale_status,
                seller_remark: listData[i].seller_remark,
                total_goods_price: listData[i].total_goods_price,
                words: listData[i].words,
                resKm: _this.getDistance(_this.data.latitude, _this.data.longitude, listData[i].latitude, listData[i].longitude),
            }
        }
        //根据距离排序
        orderAll = _this.sortArr(orderAll);
        _this.setData({
            orderLocation: markers, //组装的地图顶点
            orderAll: orderAll
        });
        console.log(orderAll)
        // 主要通知
        Notify({ type: 'primary', message: '订单加载完成' });
    },
    //根据距离计算远近
    sortArr(newArray) {
        var arr = newArray;
        var s = "";
        for (var i = 1; i < arr.length; i++) {
            for (var j = i; j > 0; j--) {
                if (arr[j].resKm > arr[j - 1].resKm) {
                    s = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = s;
                }
            }
        }
        return newArray.reverse();

    },
    // 经纬度计算距离

    getDistance(lat1, lng1, lat2, lng2) {
      
        var radLat1 = lat1 * Math.PI / 180.0;
        var radLat2 = lat2 * Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137; // EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        s = s.toFixed(2);
        return parseFloat(s);
    },

    queryOrder(e) {
        let id = e.currentTarget.dataset.id
        let user_id = e.currentTarget.dataset.user_id
        let order_id = e.currentTarget.dataset.order_id

        console.log('-----------111111111---',user_id, order_id)
        let url = app.globalData.URL
        let mall_id = app.globalData.mall_id //获取主商城id
        wx.request({
            url: url + '/deliver.Order/getViewOrderInfo',
            method: "GET",
            data: {
                id,
                mall_id
            },
            success(res) {
                // console.log(res)
                if (res.data.code === 0) {
                    let dataStr = JSON.stringify(res.data.data)
                    wx.navigateTo({
                        url: '/templates/deliver/pages/orderAll/orderAll',
                        success(res) {
                            res.eventChannel.emit('orderAll', dataStr)
                        }
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.selectShipped();
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
     //点击展开
     onAddress(event) {
        this.setData({
          activeNames: event.detail,
        });
      },
      orderBydata(event){
        var order_id = event.target.dataset.id;
        var name = event.target.dataset.name;
         wx.navigateTo({
           url: '/templates/deliver/pages/order/order?order_id='+ order_id+'&name='+name,
         })
   },
   //点击导航事件
   getLocation(e) {
    var id = e.currentTarget.dataset['id']; //获取传递的参数
    var address = this.data.orderAll[id].address; //获取地址
    var name = this.data.orderAll[id].name; //获取name
    var mobile = this.data.orderAll[id].mobile; //获取手机号
    var latitude = this.data.orderAll[id].latitude; //获取精度
    var longitude = this.data.orderAll[id].longitude //获取纬度
    
    //console.log(typeof longitude);
    wx.openLocation({
        latitude: Number(longitude), //要去的纬度-地址 一定不要使数字
        longitude: Number(latitude), //要去的经度-地址
        address: address,
        name: name + '/' + mobile,
        scale: 15
    })


},
})